package cospel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.SwingUtilities;

import utils.util;
import ventaCospeles.Venta;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ElegirCospel extends javax.swing.JFrame {
	private JLabel jLabel1;
	private JTextField fieldPatenteLetras;
	private JLabel jLabel2;
	private JTextField fieldPatenteNumeros;
	private JScrollPane jScrollPane1;
	private JScrollPane jScrollPane2;
	private JTable tableCospeles;
	private JLabel jLabel3;
	private JButton conectar;
	private JTable tablaPatentes;
	
	//Variables
	private String[][] patentes;
	private String[][] cospeles;
	
	private Cospel dataMgr;
	
	private String idParquimetro;
	
	private JFrame parent;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				ElegirCospel inst = new ElegirCospel();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public ElegirCospel() {
		super();
		setLocationRelativeTo(null);
		initGUI();
	}
	
	public ElegirCospel(JFrame parent, String idParquimetro) {
		super();
		
		this.parent = parent;
		this.idParquimetro = idParquimetro;
		
		setLocationRelativeTo(null);
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			this.setTitle("Ingreso Cospel");
			this.addWindowListener(new WindowAdapter() {
				public void windowClosing(WindowEvent evt) {
					thisWindowClosing(evt);
				}
			});
			{
				jLabel1 = new JLabel();
				getContentPane().add(jLabel1);
				jLabel1.setText("Patente:");
				jLabel1.setBounds(12, 12, 68, 23);
			}
			{
				fieldPatenteLetras = new JTextField();
				getContentPane().add(fieldPatenteLetras);
				fieldPatenteLetras.setBounds(80, 12, 33, 23);
				fieldPatenteLetras.addKeyListener(new KeyAdapter() {
					public void keyReleased(KeyEvent evt) {
						fieldPatenteLetrasKeyReleased(evt);
					}
				});
			}
			{
				jLabel2 = new JLabel();
				getContentPane().add(jLabel2);
				jLabel2.setText("-");
				jLabel2.setBounds(113, 12, 7, 23);
			}
			{
				fieldPatenteNumeros = new JTextField();
				getContentPane().add(fieldPatenteNumeros);
				fieldPatenteNumeros.setBounds(118, 12, 33, 23);
				fieldPatenteNumeros.addKeyListener(new KeyAdapter() {
					public void keyReleased(KeyEvent evt) {
						fieldPatenteNumerosKeyReleased(evt);
					}
				});
			}
			{
				jScrollPane1 = new JScrollPane();
				getContentPane().add(jScrollPane1);
				jScrollPane1.setBounds(12, 41, 139, 151);
				{
					tablaPatentes = new JTable();
					jScrollPane1.setViewportView(tablaPatentes);
					tablaPatentes.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							tablaPatentesMouseClicked(evt);
						}
					});
				}
			}
			{
				conectar = new JButton();
				getContentPane().add(conectar);
				conectar.setText("Conectar");
				conectar.setBounds(12, 203, 304, 28);
				conectar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						conectarActionPerformed(evt);
					}
				});
			}
			{
				jLabel3 = new JLabel();
				getContentPane().add(jLabel3);
				jLabel3.setText("Cospel:");
				jLabel3.setBounds(157, 12, 50, 23);
			}
			{
				jScrollPane2 = new JScrollPane();
				getContentPane().add(jScrollPane2);
				jScrollPane2.setBounds(157, 41, 158, 150);
				{
					tableCospeles = new JTable();
					jScrollPane2.setViewportView(tableCospeles);
				}
			}
			this.initData();
			
			this.fillTablaPatentes();
			pack();
			this.setSize(343, 281);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	
	/*
	 * Metodo que inicializa los datos desde la base de datos
	 */
	private void initData(){
		try{
			dataMgr =  new Cospel();
			
			this.patentes = dataMgr.getPatentes();
		}
		catch (Exception ex) {
			JOptionPane.showMessageDialog(this, ex.getMessage(), "Inspector Login", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	/*
	 * Rellena la tabla con las patentes existentes
	 */
	private void fillTablaPatentes(){		
		TableModel tablaPatentesModel = 
			new DefaultTableModel(
					this.patentes,
					new String[] { "Letras", "Numeros" });
		tablaPatentes.setModel(tablaPatentesModel);
	}
	
	private void fieldPatenteNumerosKeyReleased(KeyEvent evt) {
		String campo = fieldPatenteNumeros.getText();
		if((campo.length() > 3)||(!util.sonNumeros(campo))){
			fieldPatenteNumeros.setText(campo.substring(0, campo.length()-1));
		}
		this.patentes = dataMgr.getPatentes(this.fieldPatenteLetras.getText(), fieldPatenteNumeros.getText());
		this.fillTablaPatentes();
	}
	
	private void fieldPatenteLetrasKeyReleased(KeyEvent evt) {
		String campo = fieldPatenteLetras.getText();
		if((campo.length() > 3)||(!util.sonLetras(campo))){
			fieldPatenteLetras.setText(campo.substring(0, campo.length()-1));
		}
		this.patentes = dataMgr.getPatentes(this.fieldPatenteLetras.getText(), fieldPatenteNumeros.getText());
		this.fillTablaPatentes();
	}
	
	private void tablaPatentesMouseClicked(MouseEvent evt) {
		if(tablaPatentes.getSelectedRow() != -1){
			String letras = this.patentes[tablaPatentes.getSelectedRow()][0];
			String numeros = this.patentes[tablaPatentes.getSelectedRow()][1];
			
			this.fieldPatenteLetras.setText(letras);
			this.fieldPatenteNumeros.setText(numeros);
			this.cospeles = dataMgr.getCospeles(letras,numeros);
			this.fillTablaCospeles(this.cospeles);
		}
	}
	
	private void fillTablaCospeles(String[][] cospeles){		
		TableModel tablaPatentesModel = 
			new DefaultTableModel(
					cospeles,
					new String[] { "ID","Saldo","Tipo" });
		tableCospeles.setModel(tablaPatentesModel);
	}
	
	private void thisWindowClosing(WindowEvent evt) {
		this.parent.setVisible(true);
	}
	
	private void conectarActionPerformed(ActionEvent evt) {
		if(tableCospeles.getSelectedRow() > -1){
			String idCospel = this.cospeles[this.tableCospeles.getSelectedRow()][0];
			String result = this.dataMgr.conectar(idCospel, idParquimetro);
			
			JOptionPane.showMessageDialog(this, result, "Cospeles", JOptionPane.INFORMATION_MESSAGE);
		}else{
			JOptionPane.showMessageDialog(this, "Debe seleccionar un cospel", "Cospeles", JOptionPane.ERROR_MESSAGE);
		}
	}

}
